package com.rain.shoppingscienceproj.ui.activity.store;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.http.BaseSubscriber;
import com.rain.shoppingscienceproj.http.ExceptionHandle;
import com.rain.shoppingscienceproj.http.RetrofitClient;
import com.rain.shoppingscienceproj.module.Goods;
import com.rain.shoppingscienceproj.module.Shop;
import com.rain.shoppingscienceproj.module.StoreInfo;
import com.rain.shoppingscienceproj.ui.activity.BaseActivity;
import com.rain.shoppingscienceproj.ui.activity.ImageViewActivity;
import com.rain.shoppingscienceproj.ui.activity.shopping.ShoppingDetailsActivity;
import com.rain.shoppingscienceproj.ui.adapter.StoreAdapter;
import com.rain.shoppingscienceproj.ui.adapter.StoreShopAdapter;
import com.rain.shoppingscienceproj.ui.utile.TitleUtils;
import com.rain.shoppingscienceproj.ui.view.MyGridView;
import com.rain.shoppingscienceproj.utils.AppManager;
import com.rain.shoppingscienceproj.utils.ImageUtils;
import com.rain.shoppingscienceproj.utils.LoadingDialog;
import com.rain.shoppingscienceproj.utils.layoutmanager.CusGridLayoutManager;
import com.rain.shoppingscienceproj.utils.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * 店铺详情
 */
public class StoreListActivity extends BaseActivity {


    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @BindView(R.id.ll_data)
    LinearLayout ll_data;
    @BindView(R.id.ll_no_data)
    LinearLayout ll_no_data;

    @BindView(R.id.gv_data)
    MyGridView gv_data;

    @BindView(R.id.iv_store_iamge)
    ImageView iv_store_iamge;
    @BindView(R.id.tv_store_name)
    TextView tv_store_name;

    @BindView(R.id.tv_store_mobile)
    TextView tv_store_mobile;

    @BindView(R.id.iv_yingye)
    ImageView iv_yingye;

    private boolean isShow = true;
    private List<Goods> list = new ArrayList<>();


    String imageUrl = "";
    private int page = 1;
    private String storeId;

    private StoreShopAdapter storeShopAdapter;

    private StoreAdapter storeAdapter;


    @OnItemClick(R.id.gv_data)
    void item(int position) {
        startActivitys(ShoppingDetailsActivity.class, position + "");
    }


    @OnClick(R.id.iv_yingye)
    void image() {
        if (!"".equals(imageUrl) || null != imageUrl) {
            startActivitys(ImageViewActivity.class);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_list);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        TitleUtils.TitleView(this, "店铺详情");
        storeId = getIntent().getStringExtra("type");
        loadingDialog = new LoadingDialog(this, "正在加载...");
        initView();
        initData();
        initListener();
    }

    private void initView() {

        storeAdapter = new StoreAdapter(this);
        gv_data.setAdapter(storeAdapter);

        mRecyclerView.setLayoutManager(new CusGridLayoutManager(this, 2));
        storeShopAdapter = new StoreShopAdapter(this, 1);
        mRecyclerView.setAdapter(storeShopAdapter);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableHeaderTranslationContent(false);
//        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadmore(true);

    }

    private void initData() {
        loadingDialog.show();
        RetrofitClient.getInstance(this)
                .createBaseApi()
                .storeInfo(storeId, new BaseSubscriber<StoreInfo>(this) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {
                        ToastUtils.showShortToast(e.Message);
                        loadingDialog.dismiss();
                        ll_data.setVisibility(View.VISIBLE);
                        ll_no_data.setVisibility(View.GONE);
                    }

                    @Override
                    public void onNext(StoreInfo shop) {
                        super.onNext(shop);
                        loadingDialog.dismiss();
                        if (isShow) {
                            if (null != shop.getShop().getShopImage() || !"".equals(shop.getShop().getShopImage())) {
                                ImageUtils.image(StoreListActivity.this, shop.getShop().getShopImage(), iv_store_iamge);
                            }
                            tv_store_name.setText(shop.getShop().getShopName());
                            tv_store_mobile.setText(shop.getShop().getShopTitle());
                            if (null != shop.getShop().getShopBussinessimg() || !"".equals(shop.getShop().getShopBussinessimg())) {
                                imageUrl = shop.getShop().getShopBussinessimg();
                                ImageUtils.image(StoreListActivity.this, shop.getShop().getShopBussinessimg(), iv_store_iamge);
                            }
                        }
                        if (shop.getAllgoods().size() == 0) {
                            ll_data.setVisibility(View.GONE);
                            ll_no_data.setVisibility(View.VISIBLE);
                        } else {
                            ll_data.setVisibility(View.VISIBLE);
                            ll_no_data.setVisibility(View.GONE);
                        }
                        list.addAll(shop.getAllgoods());
                        storeAdapter.setList(shop.getGoods());
                        storeShopAdapter.setList(list);
                    }
                });



    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(refreshlayout -> refresh());
        refreshLayout.setOnLoadmoreListener(refreshlayout -> load());
        storeShopAdapter.setItemClickListener(shopId -> {
            startActivitys(ShoppingDetailsActivity.class, shopId);
        });
    }

    private void refresh() {
        list = new ArrayList<>();
        page = 1;
        isShow = false;
        initData();
        refreshLayout.finishRefresh();
    }

    private void load() {
        page++;
        isShow = false;
        initData();
        refreshLayout.finishLoadmore();
    }
}
